<?php
namespace app\manage\controller;
use  think\Db;
class Home extends Base
{
    public function slideList() {
        $type = input('post.type','');
        $curr_page = input('post.pageIndex',1);
        $perpage = input('post.pageSize',10);
        $where = [
            ['del','=',0]
        ];
        if($type) {
            $where[] = ['type','=',$type];
        }
        try {
            $count = Db::table('mp_slideshow')->where($where)->count();
            $list = Db::table('mp_slideshow')->where($where)->limit(($curr_page - 1)*$perpage,$perpage)->select();

        } catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        $ret = [
            'count' => $count,
            'list' => $list
        ];
        return ajax($ret);
    }

    public function slideDetail() {
        $post['id'] = input('post.id');
        checkPost($post);
        try {
            $where = [
                ['id','=',$post['id']]
            ];
            $exist = Db::table('mp_slideshow')->where($where)->find();
            if(!$exist) {
                return ajax('',-4);
            }
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax($exist);
    }

    public function slideAdd() {
        $post['type'] = input('post.type');
        $post['status'] = input('post.status');
        $post['title'] = input('post.title');
        checkPost($post);
        $time = time();
        $post['create_time'] = $time;
        $tmp_image = input('post.path','');
        try {
            if(!$tmp_image) {
                return ajax('请上传图片',3);
            }
            $oss_exist = $this->existObject($tmp_image);
            if($oss_exist !== true) {
                return ajax('图片已失效请重新上传',70);
            }
            $image_move = $this->moveObject($tmp_image,'upload/slide/');
            if($image_move['code'] == 0) {
                $post['pic'] = $image_move['path'];
            }else {
                return ajax('OSS异常',72);
            }
            Db::table('mp_slideshow')->insertGetId($post);
        }catch (\Exception $e) {
            if(isset($post['pic'])) {
                $this->deleteObject($post['pic']);
            }
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }

    public function slideMod() {
        $post['type'] = input('post.type');
        $post['status'] = input('post.status');
        $post['title'] = input('post.title');
        $post['id'] = input('post.id');
        checkPost($post);
        $time = time();
        $post['create_time'] = $time;
        $tmp_image = input('post.path','');
        try {
            $where = [
                ['id','=',$post['id']]
            ];
            $exist = Db::table('mp_slideshow')->where($where)->find();
            if(!$exist) {
                return ajax('',-4);
            }
            $old_pic = $exist['pic'];
            if(!$tmp_image) {
                return ajax('请上传图片',3);
            }
            $oss_exist = $this->existObject($tmp_image);
            if($oss_exist !== true) {
                return ajax('图片已失效请重新上传',70);
            }
            $image_move = $this->moveObject($tmp_image,'upload/slide/');
            if($image_move['code'] == 0) {
                $post['pic'] = $image_move['path'];
            }else {
                return ajax('OSS异常',72);
            }
            Db::table('mp_slideshow')->where($where)->update($post);
        }catch (\Exception $e) {
            if(isset($post['pic']) && $post['pic'] != $old_pic) {
                $this->deleteObject($post['pic']);
            }
            return ajax($e->getMessage(),-1);
        }
        if(isset($post['pic']) && $post['pic'] != $old_pic) {
            $this->deleteObject($old_pic);
        }
        return ajax();
    }

    public function slideDelete() {
        $post['id'] = input('post.id','');
        checkPost($post);
        $where = [
            ['id','=',$post['id']]
        ];
        try {
            Db::table('mp_slideshow')->where($where)->update(['del'=>1]);
        }catch (\Exception $e) {
            return ajax($e->getMessage(),-1);
        }
        return ajax();
    }





}